package beikecode;

import java.util.Scanner;

/**
 * Created by JiaJia on 2019/8/19 0019.
 */
public class Shangtang {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        while (scanner.hasNext()){
            int n=scanner.nextInt();
            int a=scanner.nextInt();
            int b=scanner.nextInt();
            int c=scanner.nextInt();
            int f=scanner.nextInt();
            System.out.println(Money(n,a,b,c,f));
        }
        scanner.close();
    }
    private static long Money(int n,int a,int b,int c,int f){
        long money=0;
        long[] arr=new long[n+1];
        if (n<0)
            return 0;
        if (n==0)
            return f;
        arr[1]=a*f+1+32767;
        arr[2]=a*arr[1]+b*f+6+32767;
        arr[3]=a*arr[2]+b*arr[1]+c*f+15+32767;
        for (int i = 4; i <=n ; i++) {
            arr[i]=a*arr[i-1]+b*arr[i-2]+c*arr[i-3]+2*i*i-i+32767;
        }
        return arr[n]%1000000007;
    }
}